package com.dyava.commons.exception;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by jjliu on 2017/8/27.
 */
public class ExceptionUtils extends org.apache.commons.lang3.exception.ExceptionUtils {

    public static Throwable getRealException(Throwable e){
        if(e instanceof java.lang.reflect.InvocationTargetException){
            e = ((java.lang.reflect.InvocationTargetException)e).getTargetException();
        }
        Throwable e1 = getRootCause(e);
        if(e1==null){
            return e;
        }else{
            return e1;
        }
    }

    public static String getRealMessage(Throwable e){
        if(e instanceof java.lang.reflect.InvocationTargetException){
            e = ((java.lang.reflect.InvocationTargetException)e).getTargetException();
        }
        if(e instanceof BizException){
            return  e.getMessage()+"";
        }else{
            return StringUtils.abbreviate(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e),256);
        }
    }

    public static String throwBizFastException(Throwable e){
        if(e instanceof BizFastException){
            throw (BizFastException)e;
        }else{
            throw new BizFastException(e);
        }
    }

    public static String throwRuntimeException(Throwable e){
        throw new RuntimeException(e);
     }

}
